/*
 * FRAMEWORK CODE.
 */

package min3d.sampleProject1;
import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.util.Linkify;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.AdapterContextMenuInfo;

public class MyActivityList extends ListActivity{
    private final int CONTEXTID_VIEWFILE = 0;
    private final int CONTEXTID_CANCEL = 1;
    
    private String _baseSourcePath = "http://code.google.com/p/min3d/source/browse/trunk/sampleProjects/min3dSampleProject1/src/min3d/sampleProject1/"; 
    
    class ItemVo{
        public String filename;
        public Class<?> cls;
        public String label;
        public ItemVo(String $label, Class<?> $class, String $filename){
            label = $label;
            cls = $class;
            filename = $filename;
        }
    }       
    
    /*
     * NEW ACTIVITIES MUST BE ADDED HERE AND IN THE MANIFEST.
     */
    private ItemVo[] _items = {
    	new ItemVo("Camaro", CamaroActivity.class, "CamaroActivity.java"),
    	new ItemVo("Chair", ChairActivity.class, "ChairActivity.java"),
    	new ItemVo("Watering Can", WateringcanActivity.class, "WateringcanActivity.java"),
    };
        
    @Override
    public void onCreate(Bundle savedInstanceState){
        String[] strings = new String[_items.length];
        for (int i = 0; i < _items.length; i++)
                strings[i] = _items[i].label;

        super.onCreate(savedInstanceState);
        setContentView(R.layout.splash_activity);
        setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
        TextView tv = (TextView) this.findViewById(R.id.splashTitle);
        Linkify.addLinks(tv, 0x07);
        registerForContextMenu(getListView());          
    }
    
    @Override
    public void onListItemClick(ListView parent, View v, int position, long id){
        this.startActivity( new Intent(this, _items[position].cls ) );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        return false;
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.add(0, CONTEXTID_VIEWFILE, 0, "View source on Google Code");
        menu.add(0, CONTEXTID_CANCEL, 0, "Cancel");
    }

    @Override
    public boolean onContextItemSelected(MenuItem item){
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        switch (item.getItemId()){
        case CONTEXTID_VIEWFILE:
	        Intent i = new Intent(Intent.ACTION_VIEW);
	        String url = _baseSourcePath + _items[ (int)info.id ].filename;
	        i.setData(Uri.parse(url));
	        startActivity(i);                
            return true;
        case CONTEXTID_CANCEL:
            return true;   
        default:
            return super.onContextItemSelected(item);
        }
    }    
}